/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.mzkj.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 *
 * @author ThinkGem
 * @version 2014-4-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss:sss）
     */
    public static String formatDateTimeMill(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss:sss");
    }

    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getDateTime2(Date date){
        return formatDate(date,"yyyy年MM月dd日 HH时mm分ss秒");
    }

    public static String getDateTime3(Date date){
        return formatDate(date,"yyyy年MM月dd日");
    }

    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    public static Integer getWeek(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }
    /**
     * 日期型字符串转化为日期 格式
     * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
     * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     *
     * @param date
     * @return
     */
    public static long pastHour(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     *
     * @param date
     * @return
     */
    public static long pastMinutes(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (60 * 1000);
    }

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     *
     * @param timeMillis
     * @return
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param before
     * @param after
     * @return
     */
    public static Long getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
    }

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
//		System.out.println(formatDate(parseDate("2010/3/6")));
//		System.out.println(getDate("yyyy年MM月dd日 E"));
//		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
//		System.out.println(time/(24*60*60*1000));
    }

    /**
     * 传入月份间隔数获取月份加减后的日期
     *
     * @param i
     * @return
     */
    public static String getMonthNextOrPre(Integer i) {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.MONTH, i);// 月份加减i
        Date resultDate = ca.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(resultDate);
    }

    //按照生日计算当前年龄
    public static String calAge(String birthday) {
        int months = getBabyMonths(birthday);
        String rs;
        if (months <= 0) {
            rs = "未满月";
        } else {
            int diffYear = months / 12;
            int diffMonth = months % 12;
            if (diffYear == 0) {
                rs = diffMonth + "个月";
            } else {
                if (diffMonth == 0){
                    rs = diffYear +"岁";
                }else{
                    rs = diffYear + "岁零" + diffMonth + "个月";
                }
            }
        }
        return rs;
    }

    /**
     * 传入天数 日期加减(非周日)
     * @param i
     * @return
     */
    public static Date getDayAfter(int i){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, +i);
        Date time = calendar.getTime();
        //if (DateUtils.getWeek(time) ==1){
        //    calendar.add(Calendar.DATE, +1);
        //    time = calendar.getTime();
        //}
        System.out.println(time);
        //return formatDate(time);
        return time;
    }

    /**
     * 获取当天过去几小时
     * @return
     */
    public static int getPastHour(){
        Calendar calendar = Calendar.getInstance();
        return  calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取星期几 返回文字String
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 减一秒
     * @param date
     * @return
     */
    public static Date getSubOneSec(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND,-1);
        return calendar.getTime();
    }

    /**
     * 计算宝宝总月份数
     * @param birthday
     * @return
     */
    public static int getBabyMonths(String birthday){
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(new Date());
        Calendar birthCal = Calendar.getInstance();
        birthCal.setTime(parseDate(birthday));
        int nowDay = nowCal.get(Calendar.DAY_OF_MONTH);
        int birthDay = birthCal.get(Calendar.DAY_OF_MONTH);
        int month = nowCal.get(Calendar.MONTH) - birthCal.get(Calendar.MONTH);
        int nowYear = nowCal.get(Calendar.YEAR);
        int birthYear = birthCal.get(Calendar.YEAR);
        if (nowDay - birthDay < 0){
            month = month - 1;
        }
        return month + (nowYear - birthYear)*12;
    }

    public static Date getDayAfter(Date date,int i){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, +i);
        Date time = calendar.getTime();
        return time;
    }

    public static long getMilliscond(Date date) {
        return date.getTime();
    }

    /**
     * 传入两个Date类型日期 比较大小
     * @param before
     * @param after
     * @return before=after:0 before<after:-1 before>after:1
     */
    public static int compareDate(Date before,Date after){
        return before.compareTo(after);
    }

    /**
     * 当天零点
     */
    public static Date getTodayZero(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 传入两个String类型日期 比较大小
     * @param before
     * @param after
     * @return before=after:0 before<after:-1 before>after:1
     */
    public static int compareStringDate(String before,String after){
        return DateUtils.parseDate(before).compareTo(DateUtils.parseDate(after));
    }


    /**
     * 传入一个日期 一个带单位的时间间隔 获取之后的日期
     *
     * @param date
     * @param timeUnit 单位 1:天 2:周 3:月 4:年
     * @param duration 数字
     * @return
     */
    public static Date addTimeByDurationTimeUnit(Date date,String timeUnit,Integer duration){
        if ("1".equals(timeUnit)){
            date = DateUtils.addDays(date,duration);
        }
        if ("2".equals(timeUnit)){
            date = DateUtils.addWeeks(date,duration);
        }
        if ("3".equals(timeUnit)){
            date = DateUtils.addMonths(date,duration);
        }
        if ("4".equals(timeUnit)){
            date = DateUtils.addYears(date,duration);
        }
        return date;
    }

    /**
     * 累计带单位的时长
     * @param origin 初始值
     * @param timeUnit 单位 1:天 2:周 3:月 4:年
     * @param duration 数字
     * @return
     */
    public static int addTimeWithDurationTimeUnit(int origin,String timeUnit,Integer duration){
        if (timeUnit.equals("1")){
            origin +=duration;
        }
        if (timeUnit.equals("2")){
            origin +=7 * duration;
        }
        if (timeUnit.equals("3")){
            origin += 30 * duration;
        }
        if (timeUnit.equals("4")){
            origin += 365 *duration;
        }
        return origin;
    }
}
